<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="检查人部门" prop="inspectorDepartment.id"
                :rules="[
                 ]">
          <SelectTree
                      ref="inspectorDepartment"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'children'    // 子级字段名
                        }"

                      url="/sys/office/treeData?type=2"
                      :value="inputForm.inspectorDepartment.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.inspectorDepartment.id=value}"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="检查人" prop="rummager.id"
                :rules="[
                 ]">
                <user-select :limit='1' :value="inputForm.rummager.id" @getValue='(value) => {inputForm.rummager.id=value}'></user-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="检查时间" prop="inspectionTime"
                :rules="[
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.inspectionTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="检查结果" prop="inspectionResult"
                :rules="[
                 ]">
              <el-input v-model="inputForm.inspectionResult" placeholder="请填写检查结果"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="需要整改内容" prop="rectificationContent"
                :rules="[
                 ]">
              <el-input v-model="inputForm.rectificationContent" placeholder="请填写需要整改内容"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="提交整改" prop="submitCorrective"
                :rules="[
                 ]">
              <el-input v-model="inputForm.submitCorrective" placeholder="请填写提交整改"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="是否已整改" prop="isRectified"
                :rules="[
                 ]">
                <el-select v-model="inputForm.isRectified" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('yes_no')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="已整改说明" prop="rectificationInstruction"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.rectificationInstruction" placeholder="请填写已整改说明"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="整改人" prop="rectificationPeople.id"
                :rules="[
                 ]">
                <user-select :limit='1' :value="inputForm.rectificationPeople.id" @getValue='(value) => {inputForm.rectificationPeople.id=value}'></user-select>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="图片" prop="image"
                :rules="[
                 ]">
              <el-upload ref="image"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/epc.contract/qualityimprovement/qualityImprovement`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {$window.location.href = (file.response && file.response.url) || file.url}"
                    :on-success="(response, file, fileList) => {
                       inputForm.image = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.image = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="imageArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="整改后状态" prop="postRectificationStatus"
                :rules="[
                 ]">
                <el-select v-model="inputForm.postRectificationStatus" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('post_rectification_status')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="质量检查记录" prop="qualityInspectionRecord.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择质量检查记录"
            labelName = 'jobNumber'
            labelValue = 'id'
            :value = "inputForm.qualityInspectionRecord.id"
            :limit="1"
            @getValue='(value) => {inputForm.qualityInspectionRecord.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'jobNumber',
              label: '工程编号'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'jobNumber',
              label: '工程编号'
            }
            ]"
            dataListUrl="/epc.contract/qualityinspectionrecord/qualityInspectionRecord/list"
            entityBeanName="qualityInspectionRecord"
            queryEntityUrl="/epc.contract/qualityinspectionrecord/qualityInspectionRecord/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import UserSelect from '@/components/userSelect'
  import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        imageArra: [],
        inputForm: {
          id: '',
          inspectorDepartment: {
            id: ''
          },
          rummager: {
            id: ''
          },
          inspectionTime: '',
          inspectionResult: '',
          rectificationContent: '',
          submitCorrective: '',
          isRectified: '',
          rectificationInstruction: '',
          rectificationPeople: {
            id: ''
          },
          image: '',
          postRectificationStatus: '',
          qualityInspectionRecord: {
            id: ''
          },
          remarks: ''
        }
      }
    },
    components: {
      SelectTree,
      UserSelect,
      GridSelect
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建质量整改记录`
        } else if (method === 'edit') {
          this.title = '修改质量整改记录'
        } else if (method === 'view') {
          this.title = '查看质量整改记录'
        }
        this.imageArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/epc/contract/qualityimprovement/qualityImprovement/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.qualityImprovement)
              this.inputForm.image.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.imageArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/epc/contract/qualityimprovement/qualityImprovement/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>